package cn.imvc.app.controllers.goods;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsTypeValue;
import cn.imvc.app.entities.IndexClass;
import cn.imvc.app.entities.IndexData;
import cn.imvc.app.models.GoodsModel;
import cn.imvc.app.models.GoodsTypeValueModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class GoodsTypeValueControllerImpl implements GoodsTypeValueController {
	@Autowired
	GoodsTypeValueModel goodsTypeValueModel;
	@Autowired
	GoodsModel goodsModel;
	
	public Page<GoodsTypeValue> page;
	public GoodsTypeValue goodsTypeValue;

	public ViewRequest viewRequest;
	public int id;
	public int[] ids;
	public int goodsId;
	
	public void setGoodsTypeValueModel(GoodsTypeValueModel goodsTypeValueModel) {
		this.goodsTypeValueModel = goodsTypeValueModel;
	}

	public void setPage(Page<GoodsTypeValue> page) {
		this.page = page;
	}

	public void setGoodsTypeValue(GoodsTypeValue goodsTypeValue) {
		this.goodsTypeValue = goodsTypeValue;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}

	public String search() {
		// TODO Auto-generated method stub
		return null;
	}

	public String index() {
		Map<String, String> searchMap = new HashMap<String, String>();
		if(viewRequest != null){
			searchMap = viewRequest.getParams();
		}else{
			viewRequest = new ViewRequest();
		}
		searchMap.put(" _and_g_dot_goods_dot_id_eq_string", String.valueOf(goodsId));
		viewRequest = new ViewRequest();
		viewRequest.setParams(searchMap);
		page=goodsTypeValueModel.find(viewRequest);
		return "goods-type-value/index";
	}

	public String show() {
		// TODO Auto-generated method stub
		return null;
	}

	public String editNew() {
		Goods goodsTmp = goodsModel.get(goodsId);
		goodsTypeValue = new GoodsTypeValue();
		goodsTypeValue.setGoods(goodsTmp);
		return "goods-type-value/edit";
	}

	public String create() {
		if(goodsTypeValue.getTypeValue() != null && goodsTypeValue.getTypeValue().getId() == 0){
			goodsTypeValue.setTypeValue(null);
		}
		if(goodsTypeValueModel.save(goodsTypeValue) != null){
			return Render.json(new DialogBean("200", "创建成功","goods_type_value", "closeCurrent"));
		}else{
			return Render.json(new DialogBean("300", "创建失败，已存在该类型","", ""));
		}
	}

	public String edit() {
		goodsTypeValue = goodsTypeValueModel.get(id);
		return "goods-type-value/edit";
	}

	public String update() {
		if(goodsTypeValueModel.update(goodsTypeValue) != null){
			return Render.json(new DialogBean("200", "更新成功","goods_type_value", "closeCurrent"));
		}else{
			return Render.json(new DialogBean("300", "更新失败，已存在该类型","", ""));
		}
	}

	public String destroy() {
		goodsTypeValueModel.delete(id);
		return Render.json(new DialogBean("200", "删除成功","goods_type_value"));
	}

	public String destroyBatch() {
		goodsTypeValueModel.deleteBatch(ids);
		return Render.json(new DialogBean("200", "删除成功","goods_type_value"));
	}

}
